package contents;

import contents.entities.Teacher;
import contents.entities.Timetable;
import database.Database;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class TimetablesContent {
    
    public static HttpServletRequest setContent(HttpServletRequest request) throws SQLException {
        String classIdString = request.getParameter("classId");
        if (classIdString != null) {
            int classId = Integer.valueOf(classIdString).intValue();
            request = setClassTimetableContent(classId, request);
        } else {
            int teacherId = Integer.valueOf(request.getParameter("teacherId")).intValue();
            request = setTeacherTimetableContent(teacherId, request);
        }
        return request;
    }
    
    private static HttpServletRequest setClassTimetableContent(int classId, HttpServletRequest request) throws SQLException {
        String className = Database.selectClassName(classId);
        request.setAttribute("className", className);
        
        List<Timetable> timetables = Database.selectTimetablesOfClass(classId);
        request.setAttribute("timetables", timetables);
        
        return request;
    }
        
    private static HttpServletRequest setTeacherTimetableContent(int teacherId, HttpServletRequest request) throws SQLException {
        Teacher teacher = Database.getTeacher(teacherId);
        request.setAttribute("teacher", teacher);
        
        List<Timetable> timetables = Database.selectTimetablesOfTeacher(teacherId);
        request.setAttribute("timetables", timetables);
        
        return request;
    }
    
    private TimetablesContent() {
    }
}
